import axios from "axios";
import {useCounterStore} from "@/assets/counter-index.js";

axios.defaults.baseURL = "/api";


axios.interceptors.request.use(function (config) {
    const gs = useCounterStore()
    if (gs.token){
        config.headers.token = gs.token;
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
    const resultBean = response.data;
    if (resultBean.code==500){
        return Promise.reject(resultBean.message);
    }
    if (resultBean.code==401){
        localStorage.removeItem("token");
        const gs = useCounterStore()
        gs.isLogin = false;
        gs.token = null;
        return Promise.reject(resultBean.message);
    }
    return resultBean;
}, function (error) {
    return Promise.reject(error);
});